package com.wondersgroup.framework.sample.component.demoform.action;

import java.util.ArrayList;
import java.util.List;

import com.wondersgroup.framework.core.bo.Page;
import com.wondersgroup.framework.core.exception.BusinessException;
import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.component.demoform.vo.GridSelectVO;
import com.wondersgroup.framework.sample.component.demoform.vs.DemoVS;
import com.wondersgroup.wssip.util.CollectionTools;
import com.wondersgroup.wssip.util.RequestUtils;
import com.wondersgroup.wssip.util.StringTools;

public class GridSelectAction extends BaseAjaxAction {

	private GridSelectVO gridSelectVO = new GridSelectVO();
	private DemoVS demoVS;

	public GridSelectVO getGridSelectVO() {
		return gridSelectVO;
	}

	public void setGridSelectVO(GridSelectVO gridSelectVO) {
		this.gridSelectVO = gridSelectVO;
	}

	public DemoVS getDemoVS() {
		return demoVS;
	}

	public void setDemoVS(DemoVS demoVS) {
		this.demoVS = demoVS;
	}

	@Override
	public ValueObject getValueObject() {
		return this.gridSelectVO;
	}

	@Override
	public String operate() {
		String result = prefix + suffex;
		return result;
	}

	public String loadGridSelectData() {
		String[] ids = new String[25];
		for (int i = 0; i < ids.length; i++) {
			ids[i] = String.valueOf(i + 1);
		}
		String[] names = { "luoxueni(罗雪妮)", "zengqinghua(曾清华)", "yangchu(阳楚)",
				"luoweina(罗威娜)", "luxianghong(陆向红)", "dengjun1(邓君)",
				"liuzhiming(刘志明)", "zhonghuanhuan(钟欢欢)", "zhangxueqiang(张学强)",
				"zhuguanglin(朱广灵)", "jycgly01(就业处管理员1)", "luohucs01(罗湖测试01)",
				"luohugly001(罗湖管理员001)", "xumanli(徐曼莉)", "chenminling(陈敏玲)",
				"linweizhen(林伟珍)", "xiejiajia(谢佳佳)", "wuxiangling(邬向玲)",
				"xiaoriguang(萧日光)", "lixueyan(李雪燕)", "zhangqiuxiang(张秋香)",
				"tangjuan(唐娟)", "guansisi(管思思)", "pengmengqi(彭梦琦)",
				"xiebaoer(谢宝儿)" };
		String[] sexs = { "女", "男", "男", "女", "女", "男", "男", "女", "男", "女",
				"男", "男", "男", "女", "女", "女", "女", "女", "男", "女", "女", "女",
				"女", "女", "女" };
		int[] ages = { 23, 24, 25, 26, 27, 22, 25, 33, 43, 25, 31, 19, 34, 45,
				32, 33, 45, 54, 29, 41, 54, 55, 28, 48, 32 };

		String queryText = RequestUtils.getParameter(this.servletRequest,
				"query", "");
		int start = RequestUtils.getParameter(this.servletRequest, "start", 0);
		int limit = RequestUtils.getParameter(this.servletRequest, "limit", 10);
		List<GridSelectVO> result = new ArrayList<GridSelectVO>();
		GridSelectVO vo = null;
		for (int i = 0; i < ids.length; i++) {
			vo = new GridSelectVO();
			vo.setId(ids[i]);
			vo.setName(names[i]);
			vo.setSex(sexs[i]);
			vo.setAge(ages[i]);
			if (StringTools.hasText(queryText)) {
				if (names[i].indexOf(queryText) != -1) {
					result.add(vo);
				}
			} else {
				result.add(vo);
			}
		}

		if(CollectionTools.isEmpty(result)){
			throw new BusinessException("无匹配条件【" + queryText + "】的查询结果！");
		}
		List<GridSelectVO> retList = new ArrayList<GridSelectVO>();
		int end = (start + limit < result.size()) ? start + limit : result
				.size();
		for (int i = start; i < end; i++) {
			retList.add(result.get(i));
		}

		Page page = new Page(start, end - start, result.size(), limit, retList);
		String json = VOUtils.getJsonDataFromPage(page, GridSelectVO.class);
		createJSonData(json);
		return AJAX;
	}

	public String loadFormWithGridSelect() {
		String result = "{\"people\":5" + ",\"name\":\"" + "luxianghong(陆向红)"
				+ "\"}";
		String json = prefix + result + suffex;
		createJSonData(json);
		return AJAX;
	}

	public String submitFormWithGridSelect() {
		String people = RequestUtils.getParameter(this.servletRequest,"people", "");
		System.out.println("people:"+people);
		String json = prefix + suffex;
		createJSonData(json);
		return AJAX;
	}
}
